package com.datareport.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.datareport.data.entity.DisabilityInfoNow;
import com.datareport.data.entity.DisabilityInfoPast;
import com.datareport.data.entity.ErrorInfo;
import com.datareport.data.mapper.DisabilityInfoNowMapper;
import com.datareport.data.mapper.DisabilityInfoPastMapper;
import com.datareport.data.mapper.ErrorInfoMapper;
import com.datareport.data.service.ErrorInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DataReport
 * @since 2021-11-22
 */
@Service
public class ErrorInfoServiceImpl extends ServiceImpl<ErrorInfoMapper, ErrorInfo> implements ErrorInfoService {

    @Autowired
    DisabilityInfoNowMapper disabilityInfoNowMapper;

    @Autowired
    DisabilityInfoPastMapper disabilityInfoPastMapper;

    @Autowired
    ErrorInfoMapper errorInfoMapper;

    @Autowired
    ErrorInfoService errorInfoService;

    //查询信息接口
    @Override
    public Page<ErrorInfo> selectAll(Integer pageNum, Integer pageSize) {
        Page<ErrorInfo> page = new Page<>(pageNum, pageSize);

        Page<ErrorInfo> pageParams = errorInfoMapper.selectPage(page, null);

        return pageParams;
    }

    //信息校验
    @Override
    public void check(List<String> list) {
        //解析list的校验条件
        //1、获取条件数量
        int listLenth = list.size();
        List<DisabilityInfoNow> disabilityInfoNowsList = disabilityInfoNowMapper.selectList(null);
        List<DisabilityInfoPast> disabilityInfoPastsList = disabilityInfoPastMapper.selectList(null);
        int disabilityInfoNowsListLenth = disabilityInfoNowsList.size();
        int disabilityInfoPastsListLenth = disabilityInfoPastsList.size();

        //对受教育程度的值进行转换
        //1.从未上过学 2.小学 3.初中 4.高中（含中专） 5.大学专科 6.大学本科 7.研究生
        for (int i = 0; i < disabilityInfoNowsListLenth; i++) {
            if (!StringUtils.isEmpty(disabilityInfoNowsList.get(i).getEducationLevel())) {
                if (disabilityInfoNowsList.get(i).getEducationLevel().equals("从未上过学")) {
                    disabilityInfoNowsList.get(i).setEducationLevel("1");
                }
                if (disabilityInfoNowsList.get(i).getEducationLevel().equals("小学")) {
                    disabilityInfoNowsList.get(i).setEducationLevel("2");
                }
                if (disabilityInfoNowsList.get(i).getEducationLevel().equals("初中")) {
                    disabilityInfoNowsList.get(i).setEducationLevel("3");
                }
                if (disabilityInfoNowsList.get(i).getEducationLevel().equals("高中（含中专）")) {
                    disabilityInfoNowsList.get(i).setEducationLevel("4");
                }
                if (disabilityInfoNowsList.get(i).getEducationLevel().equals("大学专科")) {
                    disabilityInfoNowsList.get(i).setEducationLevel("5");
                }
                if (disabilityInfoNowsList.get(i).getEducationLevel().equals("大学本科")) {
                    disabilityInfoNowsList.get(i).setEducationLevel("6");
                }
                if (disabilityInfoNowsList.get(i).getEducationLevel().equals("研究生")) {
                    disabilityInfoNowsList.get(i).setEducationLevel("7");
                }
            }
        }

        for (int i = 0; i < disabilityInfoPastsListLenth; i++) {
            if (!StringUtils.isEmpty(disabilityInfoPastsList.get(i).getEducationLevel())) {
                if (disabilityInfoPastsList.get(i).getEducationLevel().equals("从未上过学")) {
                    disabilityInfoPastsList.get(i).setEducationLevel("1");
                }
                if (disabilityInfoPastsList.get(i).getEducationLevel().equals("小学")) {
                    disabilityInfoPastsList.get(i).setEducationLevel("2");
                }
                if (disabilityInfoPastsList.get(i).getEducationLevel().equals("初中")) {
                    disabilityInfoPastsList.get(i).setEducationLevel("3");
                }
                if (disabilityInfoPastsList.get(i).getEducationLevel().equals("高中（含中专）")) {
                    disabilityInfoPastsList.get(i).setEducationLevel("4");
                }
                if (disabilityInfoPastsList.get(i).getEducationLevel().equals("大学专科")) {
                    disabilityInfoPastsList.get(i).setEducationLevel("5");
                }
                if (disabilityInfoPastsList.get(i).getEducationLevel().equals("大学本科")) {
                    disabilityInfoPastsList.get(i).setEducationLevel("6");
                }
                if (disabilityInfoPastsList.get(i).getEducationLevel().equals("研究生")) {
                    disabilityInfoPastsList.get(i).setEducationLevel("7");
                }
            }
        }

        //2、for循环解析
        for (int i = 0; i < listLenth; i++) {
            //2.1、分8种情况校验
            //错误类别1：受教育程度倒退
            if (list.get(i).equals("1")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）

                //2.1.2、先查找去年是否录入
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    for (int k = 0; k < disabilityInfoPastsListLenth; k++) {
                        if (disabilityInfoNowsList.get(j).getId().equals(disabilityInfoPastsList.get(k).getId())) {
                            //比较受教育程度
                            if (!StringUtils.isEmpty(disabilityInfoNowsList.get(j).getEducationLevel())
                                    && !StringUtils.isEmpty(disabilityInfoPastsList.get(k).getEducationLevel())) {
                                if (Integer.parseInt(disabilityInfoNowsList.get(j).getEducationLevel()) <
                                        Integer.parseInt(disabilityInfoPastsList.get(k).getEducationLevel())) {
                                    //存入数据库
                                    ErrorInfo errorInfo = new ErrorInfo();
                                    errorInfo.setId(disabilityInfoNowsList.get(j).getId());
                                    errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                                    errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                                    //获取当前已有错误,转为list,如果没有则创建。
                                    if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                                        errorInfo.setErrorType("1");
                                        errorInfoService.saveOrUpdate(errorInfo);
                                    } else {
                                        String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                                        List<String> errorTypeList = Arrays.asList(errorType.split(","));
                                        List newlist = new ArrayList(errorTypeList);
                                        //添加新的错误
                                        if(!newlist.contains("1")){
                                            newlist.add("1");
                                            //重新设置错误
                                            errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                            errorInfoService.saveOrUpdate(errorInfo);
                                        }
                                    }
                                }
                            }

                            if (StringUtils.isEmpty(disabilityInfoNowsList.get(j).getEducationLevel())
                                    && !StringUtils.isEmpty(disabilityInfoPastsList.get(k).getEducationLevel())
                            ) {
                                ErrorInfo errorInfo = new ErrorInfo();
                                errorInfo.setId(disabilityInfoNowsList.get(j).getId());
                                errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                                errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                                //获取当前已有错误,转为list,如果没有则创建。
                                if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                                    errorInfo.setErrorType("1");
                                    errorInfoService.saveOrUpdate(errorInfo);
                                } else {
                                    String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                                    List<String> errorTypeList = Arrays.asList(errorType.split(","));
                                    List newlist = new ArrayList(errorTypeList);
                                    //添加新的错误
                                    if(!newlist.contains("1")){
                                        newlist.add("1");
                                        //重新设置错误
                                        errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                        errorInfoService.saveOrUpdate(errorInfo);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //错误类别2：  R15.填报“4、农村危房改造政策”，R7没选填“4.自建房”
            if (list.get(i).equals("2")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    if (disabilityInfoNowsList.get(j).getReliefHousingImprovement().contains("享受住建部门农村危房改造政策")
                            && !disabilityInfoNowsList.get(j).getHouseType().equals("自建房")) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        String id = disabilityInfoNowsList.get(j).getId();
                        errorInfo.setId(id);
                        errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                        errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                        //获取当前已有错误,转为list,如果没有则创建。
                        if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                            errorInfo.setErrorType("2");
                            errorInfoService.saveOrUpdate(errorInfo);
                        } else {
                            String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                            List<String> errorTypeList = Arrays.asList(errorType.split(","));
                            List newlist = new ArrayList(errorTypeList);
                            //添加新的错误
                            if(!newlist.contains("2")){
                                newlist.add("2");
                                //重新设置错误
                                errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                errorInfoService.saveOrUpdate(errorInfo);
                            }
                        }
                    }
                }
            }

            //错误类别3：  R21.智力残疾人选填“1、康复医疗(含手术、药物)”
            if (list.get(i).equals("3")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    //获取残疾类别
                    String disabilityId = disabilityInfoNowsList.get(j).getId();
                    String pattern4 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)";
                    Pattern r4 = Pattern.compile(pattern4);
                    Matcher m4 = r4.matcher(disabilityId);
                    String disabilityCategory = "";
                    if (m4.find()) {
                        disabilityCategory = m4.group(5);
                    }

                    if (disabilityInfoNowsList.get(j).getServiceReceived().contains("康复医疗（含手术、药物）")
                            && disabilityCategory.equals("5")) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        String id = disabilityInfoNowsList.get(j).getId();
                        errorInfo.setId(id);
                        errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                        errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                        //获取当前已有错误,转为list,如果没有则创建。
                        if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                            errorInfo.setErrorType("3");
                            errorInfoService.saveOrUpdate(errorInfo);
                        } else {
                            String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                            List<String> errorTypeList = Arrays.asList(errorType.split(","));
                            List newlist = new ArrayList(errorTypeList);
                            //添加新的错误
                            if(!newlist.contains("3")){
                                newlist.add("3");
                                //重新设置错误
                                errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                errorInfoService.saveOrUpdate(errorInfo);
                            }
                        }
                    }
                }
            }

            //错误类别4：  R21.智力残疾人选填“3、辅助器具”
            if (list.get(i).equals("4")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    //获取残疾类别
                    String disabilityId = disabilityInfoNowsList.get(j).getId();
                    String pattern4 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)";
                    Pattern r4 = Pattern.compile(pattern4);
                    Matcher m4 = r4.matcher(disabilityId);
                    String disabilityCategory = "";
                    if (m4.find()) {
                        disabilityCategory = m4.group(5);
                    }

                    if (disabilityInfoNowsList.get(j).getServiceReceived().contains("辅助器具")
                            && disabilityCategory.equals("5")) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        errorInfo.setId(disabilityInfoNowsList.get(j).getId());
                        errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                        errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                        //获取当前已有错误,转为list,如果没有则创建。
                        if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                            errorInfo.setErrorType("4");
                            errorInfoService.saveOrUpdate(errorInfo);
                        } else {
                            String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                            List<String> errorTypeList = Arrays.asList(errorType.split(","));
                            List newlist = new ArrayList(errorTypeList);
                            //添加新的错误
                            if(!newlist.contains("4")){
                                newlist.add("4");
                                //重新设置错误
                                errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                errorInfoService.saveOrUpdate(errorInfo);
                            }
                        }
                    }
                }
            }

            //错误类别5：  R21.精神残疾人选填“3、辅助器具”
            if (list.get(i).equals("5")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    //获取残疾类别
                    String disabilityId = disabilityInfoNowsList.get(j).getId();
                    String pattern4 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)";
                    Pattern r4 = Pattern.compile(pattern4);
                    Matcher m4 = r4.matcher(disabilityId);
                    String disabilityCategory = "";
                    if (m4.find()) {
                        disabilityCategory = m4.group(5);
                    }

                    if (disabilityInfoNowsList.get(j).getServiceReceived().contains("辅助器具")
                            && disabilityCategory.equals("6")) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        errorInfo.setId(disabilityInfoNowsList.get(j).getId());
                        errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                        errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                        if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                            errorInfo.setErrorType("5");
                            errorInfoService.saveOrUpdate(errorInfo);
                        } else {
                            String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                            List<String> errorTypeList = Arrays.asList(errorType.split(","));
                            List newlist = new ArrayList(errorTypeList);
                            //添加新的错误
                            if(!newlist.contains("5")){
                                newlist.add("5");
                                //重新设置错误
                                errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                errorInfoService.saveOrUpdate(errorInfo);
                            }
                        }
                    }
                }
            }

            //错误类别6：  R22.智力残疾人选填“1、康复医疗(含手术、药物)”
            if (list.get(i).equals("6")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    //获取残疾类别
                    String disabilityId = disabilityInfoNowsList.get(j).getId();
                    String pattern4 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)";
                    Pattern r4 = Pattern.compile(pattern4);
                    Matcher m4 = r4.matcher(disabilityId);
                    String disabilityCategory = "";
                    if (m4.find()) {
                        disabilityCategory = m4.group(5);
                    }

                    if (disabilityInfoNowsList.get(j).getServiceReceived().contains("康复医疗（含手术、药物）")
                            && disabilityCategory.equals("5")) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        errorInfo.setId(disabilityInfoNowsList.get(j).getId());
                        errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                        errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                        //获取当前已有错误,转为list,如果没有则创建。
                        if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                            errorInfo.setErrorType("6");
                            errorInfoService.saveOrUpdate(errorInfo);
                        } else {
                            String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                            List<String> errorTypeList = Arrays.asList(errorType.split(","));
                            List newlist = new ArrayList(errorTypeList);
                            //添加新的错误
                            if(!newlist.contains("6")){
                                newlist.add("6");
                                //重新设置错误
                                errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                errorInfoService.saveOrUpdate(errorInfo);
                            }
                        }
                    }
                }
            }

            //错误类别7：  R22.智力残疾人选填“3、辅助器具”
            if (list.get(i).equals("7")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    //获取残疾类别
                    String disabilityId = disabilityInfoNowsList.get(j).getId();
                    String pattern4 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)";
                    Pattern r4 = Pattern.compile(pattern4);
                    Matcher m4 = r4.matcher(disabilityId);
                    String disabilityCategory = "";
                    if (m4.find()) {
                        disabilityCategory = m4.group(5);
                    }

                    if (disabilityInfoNowsList.get(j).getServiceDemand().contains("辅助器具")
                            && disabilityCategory.equals("5")) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        errorInfo.setId(disabilityInfoNowsList.get(j).getId());
                        errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                        errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                        //获取当前已有错误,转为list,如果没有则创建。
                        if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                            errorInfo.setErrorType("7");
                            errorInfoService.saveOrUpdate(errorInfo);
                        } else {
                            String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                            List<String> errorTypeList = Arrays.asList(errorType.split(","));
                            List newlist = new ArrayList(errorTypeList);
                            //添加新的错误
                            if(!newlist.contains("7")){
                                newlist.add("7");
                                //重新设置错误
                                errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                errorInfoService.saveOrUpdate(errorInfo);
                            }
                        }
                    }
                }
            }

            //错误类别8：  R22.精神残疾人选填“3、辅助器具”
            if (list.get(i).equals("8")) {
                //2.1.1、校验后存入数据库，（并修改原表的check值）
                for (int j = 0; j < disabilityInfoNowsListLenth; j++) {
                    //获取残疾类别
                    String disabilityId = disabilityInfoNowsList.get(j).getId();
                    String pattern4 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)";
                    Pattern r4 = Pattern.compile(pattern4);
                    Matcher m4 = r4.matcher(disabilityId);
                    String disabilityCategory = "";
                    if (m4.find()) {
                        disabilityCategory = m4.group(5);
                    }

                    if (disabilityInfoNowsList.get(j).getServiceDemand().contains("辅助器具")
                            && disabilityCategory.equals("6")) {
                        ErrorInfo errorInfo = new ErrorInfo();
                        errorInfo.setId(disabilityInfoNowsList.get(j).getId());
                        errorInfo.setRegionId(disabilityInfoNowsList.get(j).getRegionId().toString());
                        errorInfo.setName(disabilityInfoNowsList.get(j).getName());
                        //获取当前已有错误,转为list,如果没有则创建。
                        if (errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()) == null) {
                            errorInfo.setErrorType("8");
                            errorInfoService.saveOrUpdate(errorInfo);
                        } else {
                            String errorType = errorInfoMapper.selectById(disabilityInfoNowsList.get(j).getId()).getErrorType();
                            List<String> errorTypeList = Arrays.asList(errorType.split(","));
                            List newlist = new ArrayList(errorTypeList);
                            //添加新的错误
                            if(!newlist.contains("8")){
                                newlist.add("8");
                                //重新设置错误
                                errorInfo.setErrorType(StringUtils.join(newlist, ","));
                                errorInfoService.saveOrUpdate(errorInfo);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public List<ErrorInfo> exportList(int i, int max) {

        List<ErrorInfo> list = errorInfoMapper.selectList(new QueryWrapper<ErrorInfo>().between("id", i + 1, max));
        return list;
    }

}
